package com.dcy.znoa.controller.publiz;

import com.aliyuncs.exceptions.ClientException;
import com.dcy.znoa.common.annotation.AuthenticationAPI;
import com.dcy.znoa.common.controller.BaseController;
import com.dcy.znoa.common.exception.BusinessException;
import com.dcy.znoa.common.util.pojo.JsonCommon;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.controller.publiz.model.messNotice.*;
import com.dcy.znoa.domain.publiz.MessNotice;
import com.dcy.znoa.service.publiz.MessNoticeService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;

/**
 * @author pengyu
 */
@Controller
@RequestMapping("messNotice")
public class MessNoticeController extends BaseController {
    @Autowired
    MessNoticeService messNoticeService;

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectList")
    public JsonForList selectList(@Valid SelectMessNoticeModel model) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if(model.getMessType()==null){
            model.setMessType(6);
        }
        return successPage(messNoticeService.selectList(model));
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectUnReadList")
    public JsonForList selectUnReadList(@Valid SelectByReceiveOperIdModel model,BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            return  new JsonForList("404",bindingResult.getFieldError().getDefaultMessage());
        }
        return successPage(messNoticeService.selectUnReadList(model));
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectReadList")
    public JsonForList selectReadList(@Valid SelectByReceiveOperIdModel model,BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            return  new JsonForList("404",bindingResult.getFieldError().getDefaultMessage());
        }
        return successPage(messNoticeService.selectReadList(model));
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/insert")
    public JsonCommon insert(@Valid AddMessNoticeModel model,BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException, ClientException {
        if (bindingResult.hasErrors()){
            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
        }
        MessNotice cp = new MessNotice();
        BeanUtils.copyProperties(cp, model);
        messNoticeService.insert(cp);
        return JsonCommon.SUCCESS;
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/updateById")
    public JsonCommon updateById(@Valid UpdateMessNoticeModel model,BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
        }
        MessNotice cp = new MessNotice();
        BeanUtils.copyProperties(cp, model);
        messNoticeService.updateById(cp);
        return JsonCommon.SUCCESS;
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/logicDeleteById")
    public JsonCommon logicDeleteById(@Valid LogicDelMessNoticeModel model,BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
        }
        MessNotice cp = new MessNotice();
        BeanUtils.copyProperties(cp, model);
        messNoticeService.updateById(cp);
        return JsonCommon.SUCCESS;
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/insertBatch")
    public JsonCommon insertBatch(@Valid AddMessNoticeListModel model,BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException, ClientException {
        if (bindingResult.hasErrors()){
            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
        }
        messNoticeService.insertBatch(model);
        return JsonCommon.SUCCESS;
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/insertBatchAndMsg")
    public JsonCommon insertBatchAndMsg(@Valid AddMessNoticeListAndMsgModel model,BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException, ClientException {
        if (bindingResult.hasErrors()){
            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
        }
        model.setCreateOperId(getCurrentUserId());
        messNoticeService.insertBatchAndMsg(model);
        return JsonCommon.SUCCESS;
    }

    //暂时不用这个方法
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/updateBatch")
    public JsonCommon updateBatch(@Valid UpdateMessNoticeListModel model,BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
        }
        messNoticeService.updateBatch(model);
        return JsonCommon.SUCCESS;
    }

}
